package com.huluobo.a2301mdmdemo.demo

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

/**
 *  Created by LC on 2025/6/20.
 *  跳转传值
 *  NavHost 导航的宿主,一个宿主对应一组它声明过的页面
 *  路径地址 一定要写一个通用的文件来调用,防止跳转找不到地址
 *  传值 声明时需要通过/{} 来声明变量,传值时需要去掉大括号
 *  同时,navHostController 通过/来判断地址或者变量,在传值的同时尽量不要有斜杠,必须要有的时候需要加转义符
 */

@Composable
fun MyNavigation() {
    //导航控制器
    val navHostController = rememberNavController()


    NavHost(
        //导航控制器
        navController = navHostController,
        //起始页地址
        startDestination = RoutePahDemo.MAIN,
        //声明该导航中所有的页面
        builder = {
            composable(RoutePahDemo.MAIN) { MainPage(navHostController) }
            composable(RoutePahDemo.SEC + "/{string}") {
                //声明传值的参数
                val str = it.arguments?.getString("string").toString()
                SecondPage(navHostController = navHostController, str = str)
            }
        })
}

@Composable
fun MainPage(navHostController: NavHostController) {
    Text(text = "主页面", modifier = Modifier
        .fillMaxSize()
        .background(Color.Yellow)
        .clickable {
            //跳转
            navHostController.navigate(RoutePahDemo.SEC + "/123")
        })
}

@Composable
fun SecondPage(navHostController: NavHostController, str: String) {
    Text(text = "第二页面+$str", modifier = Modifier
        .fillMaxSize()
        .background(Color.Green)
        .clickable {
            //跳转回主页面,相当于new出一个的页面
//            navHostController.navigate("main")
            //返回到上级页面,销毁当前页面
            navHostController.popBackStack()
        })
}